<script setup lang="ts">
import { Api } from '@/assets/apis';
import { ref, onMounted } from 'vue'
import type { HomeDataType } from '@/typings'
import { Toast } from 'vant';
import { useRouter } from 'vue-router';


const homeData = ref<Array<HomeDataType>>([])
const loading = ref(false)
const router = useRouter()

const book: [{ title: string, author: string, coverImage: string, id: string }] = JSON.parse(localStorage.getItem('bookshelf') as string) || []

onMounted(async () => {
    Toast.loading({
        message: '加载中...',
        forbidClick: true,
        duration: 0,
    });
    let { data } = await Api.getBanner()
    console.log(data, '111');

    homeData.value = data.data.list
    console.log(data.data);
    Toast.clear()
    loading.value = true
})

const detail = (id: string, title: string) => {

    router.push(`/book/${id}?title=${title}`)
}
</script>

<template>
    <div v-show="loading">
        <div class="banner">
            <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
                <van-swipe-item class="swipe-item" v-for="a in homeData[0]?.banners">
                    <img :src="a.cover" alt="">
                </van-swipe-item>
            </van-swipe>
        </div>
        <div class="history" @click="router.push('/bookshelf')">
            <div class="last-book">
                <van-icon name="clock-o" />
                {{ book[0]?.title }}
            </div>
            <div class="book">
                书架
                <van-icon name="arrow" />
            </div>
        </div>
        <div class="list">
            <div class="title">
                <div class="title-name">主编推荐</div>
                <div class="more">
                    更多
                </div>
            </div>
            <div class="book-list">
                <div class="item" v-for="a in homeData[3]?.list" @click="detail(a.id, a.title)">
                    <div class="poster">
                        <img :src="a.cover" alt="">
                    </div>
                    <div class="name">{{ a.title }}</div>
                    <div class="author">{{ a.author }}</div>
                </div>
            </div>
            <div class="book-list">
                <div class="item" v-for="a in homeData[5]?.list" @click="detail(a.id, a.title)">
                    <div class="poster">
                        <img :src="a.cover" alt="">
                    </div>
                    <div class="name">{{ a.title }}</div>
                    <div class="author">{{ a.author }}</div>
                </div>
            </div>
        </div>
        <div class="list">
            <div class="title">
                <div class="title-name">新书上线</div>
                <div class="more">
                    更多
                </div>
            </div>
            <div class="book-list">
                <div class="item" v-for="a in homeData[6]?.list" @click="detail(a.id, a.title)">
                    <div class="poster">
                        <img :src="a.cover" alt="">
                    </div>
                    <div class="name">{{ a.title }}</div>
                    <div class="author">{{ a.author }}</div>
                </div>
            </div>
            <div class="book-list">
                <div class="item" v-for="a in homeData[7]?.list" @click="detail(a.id, a.title)">
                    <div class="poster">
                        <img :src="a.cover" alt="">
                    </div>
                    <div class="name">{{ a.title }}</div>
                    <div class="author">{{ a.author }}</div>
                </div>
            </div>
        </div>
    </div>
</template>

<style scoped lang="scss">
.banner {
    width: 335rem;
    height: 126rem;
    padding: 0 20rem;
    margin-top: 10rem;

    .swipe-item {
        height: 126rem;

        img {
            width: 100%;
            height: 100%;
            border-radius: 5rem;
        }
    }
}

.history {
    width: 335rem;
    height: 56rem;
    margin: 0 20rem;
    display: flex;
    justify-content: space-between;
    line-height: 56rem;
    text-align: center;
    font-size: 15rem;
    color: #595550;
    border-bottom: 1rem solid #ededed;
}

.list {
    width: 335rem;
    // height: 656rem;
    padding: 15rem 0 30rem 0;
    margin: 0 20rem 4rem 20rem;
    border-bottom: 1rem solid #ededed;

    .title {
        width: 335rem;
        height: 28rem;
        margin-bottom: 16rem;
        display: flex;
        justify-content: space-between;
        line-height: 28rem;

        .title-name {
            font-size: 20rem;
            font-weight: bold;
            color: #1a1a1a;
        }

        .more {
            color: #999;
            font-size: 13rem;
            border: 1rem solid #f0f0f0;
            padding: 0 17rem;
            border-radius: 28rem;
        }
    }

    .book-list {
        width: 335rem;
        overflow: hidden;
        // height: 219rem;
        display: flex;

        .item {
            width: 93rem;
            height: 200rem;
            margin: 0 26rem 20rem 0;

            .poster {
                width: 93rem;
                height: 131rem;

                img {
                    width: 100%;
                    height: 100%;
                    border-radius: 5rem;
                }
            }

            .name {
                width: 93rem;
                height: 38rem;
                margin: 10rem 0 5rem 0;
                font-weight: bold;
                font-size: 15rem;
                line-height: 19rem;
                color: #222;
                overflow: hidden;
                text-overflow: ellipsis;
                display: -webkit-box;
                -webkit-line-clamp: 2;
                -webkit-box-orient: vertical;
            }

            .author {
                font-size: 12rem;
                color: #888;
                overflow: hidden;
                text-overflow: ellipsis;
                white-space: nowrap;
            }
        }
    }
}
</style>